//
//  ZNTabBarController.m
//  ZNTabBarController
//
//  Created by Sniper on 15/12/23.
//  Copyright © 2015年 Sniper. All rights reserved.
//

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "ZNTabBarController.h"
#import "ZNTabBarItem.h"
#import <objc/runtime.h>

@interface UIViewController (ZNTabBarControllerItemInternal)

- (void)zn_setTabBarController:(ZNTabBarController *)tabBarController;

@end

@interface ZNTabBarController ()<ZNTabBarDelegate> {
    UIView *_contentView;
}

@property (nonatomic, readwrite) ZNTabBar *tabbar;

//- (void)zn_setTabBarController:(ZNTabBarController *)tabBarController;

@end

@implementation ZNTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.tabbar = [[ZNTabBar alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 49, [UIScreen mainScreen].bounds.size.width, 49)];
    self.tabbar.delegate = self;
    self.view.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:[self contentView]];

    NSMutableArray *tabBarItems = [NSMutableArray arrayWithCapacity:0];
    
    for (NSInteger i = 0; i < 3; i++) {
        ZNTabBarItem *tabBarItem = [[ZNTabBarItem alloc] init];
        [tabBarItem setTabbarTitle:self.title];
        [tabBarItems addObject:tabBarItem];
    }
    
    [self.tabbar setItemsArray:tabBarItems];
    
    [self.view addSubview:self.tabbar];
}

-(void)tabbar:(ZNTabBar *)tabbar didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"index %ld", index);
    if (index < 0 || index >= [[self viewControllers] count]) {
        return;
    }
    
    [self setSelectedIndex:index];
    
    if ([[self delegate] respondsToSelector:@selector(tabBarController:didSelectViewController:)]) {
        [[self delegate] tabBarController:self didSelectViewController:[self viewControllers][index]];
    }

}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    if (selectedIndex >= self.viewControllers.count) {
        return;
    }
    
    if ([self selectedViewController]) {
        [[self selectedViewController] willMoveToParentViewController:nil];
        [[[self selectedViewController] view] removeFromSuperview];
        [[self selectedViewController] removeFromParentViewController];
    }
    
    _selectedIndex = selectedIndex;
    [[self tabbar] setSelectedItem:[[self tabbar] itemsArray][selectedIndex]];
    
    [self setSelectedViewController:[[self viewControllers] objectAtIndex:selectedIndex]];
    [self addChildViewController:[self selectedViewController]];
    [[[self selectedViewController] view] setFrame:[[self contentView] bounds]];
    [[self contentView] addSubview:[[self selectedViewController] view]];
    [[self selectedViewController] didMoveToParentViewController:self];
    
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)setViewControllers:(NSArray *)viewControllers {
    if (_viewControllers && _viewControllers.count) {
        for (UIViewController *viewController in _viewControllers) {
            [viewController willMoveToParentViewController:nil];
            [viewController.view removeFromSuperview];
            [viewController removeFromParentViewController];
        }
    }
    
    if (viewControllers && [viewControllers isKindOfClass:[NSArray class]]) {
        _viewControllers = [viewControllers copy];
        
        NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];
        
        for (UIViewController *viewController in viewControllers) {
            ZNTabBarItem *tabBarItem = [[ZNTabBarItem alloc] init];
//            [tabBarItem setTitle:viewController.title];
//            [tabBarItem setShowTitleEmbeded:_showTitleEmbeded];
            [tabBarItems addObject:tabBarItem];
            [viewController zn_setTabBarController:self];
        }
        
        [[self tabbar] setItemsArray:tabBarItems];
    } else {
//        for (UIViewController *viewController in _viewControllers) {
//            [viewController rdv_setTabBarController:nil];
//        }
        
        _viewControllers = nil;
    }
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        [_contentView setBackgroundColor:[UIColor whiteColor]];
        [_contentView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|
                                           UIViewAutoresizingFlexibleHeight)];
    }
    return _contentView;
}

- (ZNTabBar *)tabbar {
    if (!_tabbar) {
        _tabbar = [[ZNTabBar alloc] init];
        [_tabbar setBackgroundColor:[UIColor clearColor]];
        [_tabbar setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|
                                      UIViewAutoresizingFlexibleTopMargin|
                                      UIViewAutoresizingFlexibleLeftMargin|
                                      UIViewAutoresizingFlexibleRightMargin|
                                      UIViewAutoresizingFlexibleBottomMargin)];
        [_tabbar setDelegate:self];
    }
    return _tabbar;
}

- (NSInteger)indexForViewController:(UIViewController *)viewController {
    UIViewController *searchedController = viewController;
    if ([searchedController navigationController]) {
        searchedController = [searchedController navigationController];
    }
    return [[self viewControllers] indexOfObject:searchedController];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

@implementation UIViewController (ZNTabBarControllerItemInternal)

- (void)zn_setTabBarController:(ZNTabBarController *)tabBarController {
    objc_setAssociatedObject(self, @selector(zn_tabBarController), tabBarController, OBJC_ASSOCIATION_ASSIGN);
}

@end

@implementation UIViewController (ZNTabBarControllerItem)

- (ZNTabBarController *)zn_tabBarController {
    ZNTabBarController *tabBarController = objc_getAssociatedObject(self, @selector(zn_tabBarController));
    
    if (!tabBarController && self.parentViewController) {
        tabBarController = [self.parentViewController zn_tabBarController];
    }
    
    return tabBarController;
}

- (ZNTabBarItem *)rdv_tabBarItem {
    ZNTabBarController *tabBarController = [self zn_tabBarController];
    NSInteger index = [tabBarController indexForViewController:self];
    return [[[tabBarController tabbar] itemsArray] objectAtIndex:index];
}

- (void)zn_setTabBarItem:(ZNTabBarItem *)tabBarItem {
    ZNTabBarController *tabBarController = [self zn_tabBarController];
    
    if (!tabBarController) {
        return;
    }
    
    ZNTabBar *tabBar = [tabBarController tabbar];
    NSInteger index = [tabBarController indexForViewController:self];
    
    NSMutableArray *tabBarItems = [[NSMutableArray alloc] initWithArray:[tabBar itemsArray]];
    [tabBarItems replaceObjectAtIndex:index withObject:tabBarItem];
    [tabBar setItemsArray:tabBarItems];
}

@end

